﻿/*
 * @文件描述:语音识别的程序,用于识别关键字并控制电机
 * @版本:
 * @作者:徐俊鑫
 * @Date:2021-09-22 18:09:56
 */

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Windows.Speech;//引入命名空间  利用
using myConection;
using Motor_Control;
/// <summary>
/// 语音识别（主要是别关键字）
/// </summary>
public class speechKey : MonoBehaviour
{
    // 短语识别器
    private PhraseRecognizer m_PhraseRecognizer;
    // 关键字
    public string[] keywords = { "上", "下", "左", "右" };
    // 可信度
    public ConfidenceLevel m_confidenceLevel = ConfidenceLevel.Medium;//识别的可信度默认为中等

    private connection connection;
    private GameObject conObj;
    private motorControl motorControl;

    /// <summary>
    /// 显示的文字
    /// </summary>
    private Text ShowText;

    //***************************************用于关键字判断
    public bool up_bool = false;
    public bool down_bool = false;
    public bool left_bool = false;
    public bool right_bool = false;

    //public string temp;

    public Image up_image;
    public Image down_image;
    public Image left_image;
    public Image right_image;
    //***************************************用于关键字判断

    void Awake()
    {

    }

    void Start()
    {
        conObj = GameObject.Find("GameObject");
        //获取连接控制对象
        connection = (myConection.connection)conObj.GetComponent(typeof(myConection.connection));
        //获取电机控制对象
        motorControl = (motorControl)conObj.GetComponent(typeof(motorControl));
        //todo:这个后期需要移动到view中处理
        ShowText = GameObject.Find("ShowText").GetComponent<Text>();
        if (m_PhraseRecognizer == null)
        {
            //创建一个识别器
            m_PhraseRecognizer = new KeywordRecognizer(keywords, m_confidenceLevel);
            //通过注册监听的方法
            m_PhraseRecognizer.OnPhraseRecognized += M_PhraseRecognizer_OnPhraseRecognized;
            // m_PhraseRecognizer.OnPhraseRecognized += SendLeftMessage;
            // m_PhraseRecognizer.OnPhraseRecognized += SendRightMessage;
            // m_PhraseRecognizer.OnPhraseRecognized += SendUpMessage;
            // m_PhraseRecognizer.OnPhraseRecognized += SendDownMessage;
            //开启识别器
            m_PhraseRecognizer.Start();

            Debug.Log("创建识别器成功");
        }


    }




    /// <summary>
    ///  当识别到关键字时，会调用这个方法,然后会直接控制电机运动
    /// </summary>
    /// <param name="args"></param>
    private void M_PhraseRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
    {
        ShowText.text = "识别内容：" + args.text;
        print(args.text);
        switch (args.text)
        {
            case "上":
                motorControl.HandControl(HAND_DIRECTION.UP, 10000);

                break;
            case "下":
                motorControl.HandControl(HAND_DIRECTION.DOWN, 10000);

                break;

            case "左":
                motorControl.HandControl(HAND_DIRECTION.LEFT, 10000);

                break;

            case "右":
                motorControl.HandControl(HAND_DIRECTION.RIGHT, 10000);

                break;

        }
        m_PhraseRecognizer.Stop();

    }
    private void OnDestroy()
    {
        //判断场景中是否存在语音识别器，如果有，释放
        if (m_PhraseRecognizer != null)
        {
            //用完应该释放，否则会带来额外的开销
            m_PhraseRecognizer.Dispose();
        }

    }

    /// <summary>
    /// 进行关键字判断
    /// </summary>
    private void Update()
    {
        if (ShowText.text.IndexOf("上") >= 0)//只要识别的语句中包括”上“   下面同理
        {
            up_bool = true;
            down_bool = false;
            left_bool = false;
            right_bool = false;
        }
        if (ShowText.text.IndexOf("下") >= 0)
        {
            down_bool = true;
            up_bool = false;
            left_bool = false;
            right_bool = false;
        }
        if (ShowText.text.IndexOf("左") >= 0)
        {
            left_bool = true;
            up_bool = false;
            down_bool = false;
            right_bool = false;

        }
        if (ShowText.text.IndexOf("右") >= 0)
        {
            right_bool = true;
            up_bool = false;
            down_bool = false;
            left_bool = false;

        }
        light_image();
        if (!m_PhraseRecognizer.IsRunning)
        {
            m_PhraseRecognizer.Start();
        }
    }


    /// <summary>
    /// 识别关键字”上下左右“的反馈机制
    /// </summary>
    private void light_image()
    {
        if (up_bool == true)
        {
            up_image.GetComponent<Image>().color = new Color(0.5f, 0.8f, 0.1f, 1.0f);
            down_image.GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            left_image.GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            right_image.GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            down_bool = false;
            left_bool = false;
            right_bool = false;

        }
        if (down_bool == true)
        {
            up_image.GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            down_image.GetComponent<Image>().color = new Color(0.5f, 0.8f, 0.1f, 1.0f);
            left_image.GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            right_image.GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            up_bool = false;
            left_bool = false;
            right_bool = false;
        }
        if (left_bool == true)
        {
            up_image.GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            down_image.GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            left_image.GetComponent<Image>().color = new Color(0.5f, 0.8f, 0.1f, 1.0f);
            right_image.GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            up_bool = false;
            down_bool = false;
            right_bool = false;

        }
        if (right_bool == true)
        {
            up_image.GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            down_image.GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            left_image.GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            right_image.GetComponent<Image>().color = new Color(0.5f, 0.8f, 0.1f, 1.0f);
            up_bool = false;
            down_bool = false;
            left_bool = false;

        }
    }

    // void SendLeftMessage(PhraseRecognizedEventArgs args)
    // {
    //     Debug.Log("SendLeftMessage" + args.text);
    //     if (args.text == "左")
    //         connection.sendMessage("0");
    //     m_PhraseRecognizer.Stop();
    // }
    // void SendRightMessage(PhraseRecognizedEventArgs args)
    // {
    //     Debug.Log("SendRightMessage" + args.text);
    //     if (args.text == "右")
    //         connection.sendMessage("1");
    //     m_PhraseRecognizer.Stop();
    // }
    // void SendUpMessage(PhraseRecognizedEventArgs args)
    // {
    //     Debug.Log("SendRightMessage" + args.text);
    //     if (args.text == "上")
    //         connection.sendMessage("2");
    //     m_PhraseRecognizer.Stop();
    // }
    // void SendDownMessage(PhraseRecognizedEventArgs args)
    // {
    //     Debug.Log("SendRightMessage" + args.text);
    //     if (args.text == "下")
    //         connection.sendMessage("3");
    //     m_PhraseRecognizer.Stop();
    // }
}